﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Enterprise.Model.WorkFlow;
using Enterprise.Data.WorkFlow;
using Enterprise.Infrastructure;
using System.Data;
namespace Enterprise.Service.WorkFlow
{
    /// <summary>
    /// 流程节点业务服务
    /// </summary>
    public class WorkFlowNodeService
    {
        /// <summary>
        /// 流程节点缓存标记
        /// </summary>
        private static string cacheKey = "Enterprise-WorkFlowNode";

        /// <summary>
        /// 流程列表 返回所有流程记录
        /// </summary>
        /// <returns></returns>
        public List<WorkFlowNodeEntity> WorkFlowNodeList()
        {
            if (CacheHelper.Contains(cacheKey))
            {
                return (List<WorkFlowNodeEntity>)CacheHelper.GetCache(cacheKey);
            }
            else
            {
                WorkFlowNodeData data = new WorkFlowNodeData();
                List<WorkFlowNodeEntity> list = data.WorkFlowNodeList();
                CacheHelper.Add(cacheKey, list);
                return list;
            }
        }

        /// <summary>
        /// 返回流程记录信息
        /// </summary>
        /// <param name="Id">流程编码</param>
        /// <returns></returns>
        public List<WorkFlowNodeEntity> WorkFlowNode(int Id)
        {
            return WorkFlowNodeList().Where(p => p.WFID == Id).OrderBy(p => p.NORDERBY).ToList();
        }

        /// <summary>
        /// 返回流程最大排序编号
        /// </summary>
        /// <param name="wfId"></param>
        /// <returns></returns>
        public int WorkFlowNodeMaxOrderBy(int wfId)
        {
            var q = WorkFlowNode(wfId).LastOrDefault();
            if (q != null)
            {
                return q.NORDERBY;
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// 清楚缓存
        /// </summary>
        private void ClearCache()
        {
            CacheHelper.RemoveCache(cacheKey);
        }

        /// <summary>
        /// 流程操作 添加、修改、删除
        /// </summary>
        /// <param name="entity">用户实体</param>
        public void WorkFlowNodeExecute(WorkFlowNodeEntity entity)
        {
            string cmd = string.Empty;
            switch (entity.DB_Option_Action)
            {
                case "Insert":
                    cmd = "Insert into SYS_WORKFLOWNODE(NID,WFID,NNAME,NTYPE,NOBJECTTYPE,NOBJECT,NREMARK,NORDERBY) values(SEQ_SYS_WORKFLOWNODE.nextval," + entity.WFID + ",'" + entity.NNAME + "'," + entity.NTYPE + "," + entity.NOBJECTTYPE + "," + entity.NOBJECT + ",'" + entity.NREMARK + "'," + entity.NORDERBY + ")";
                    break;
                case "Update":
                    cmd = "Update SYS_WORKFLOWNODE set NNAME='" + entity.NNAME + "' , NTYPE=" + entity.NTYPE + " , NOBJECTTYPE=" + entity.NOBJECTTYPE + ",NOBJECT=" + entity.NOBJECT + " , NREMARK='" + entity.NREMARK + "' where NID=" + entity.NID;
                    break;
                case "Delete":
                    cmd = "Delete from SYS_WORKFLOWNODE where NID=" + entity.NID;
                    break;
            }
            if (!string.IsNullOrEmpty(cmd))
            {
                WorkFlowNodeExecuteBySql(cmd, 1);
            }
        }

        /// <summary>
        /// 操作方法
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="typeId">1:刷新缓存 0:不刷新缓存 </param>
        public void WorkFlowNodeExecuteBySql(string cmd, int typeId)
        {
            WorkFlowNodeData data = new WorkFlowNodeData();
            data.WorkFlowExecute(cmd);
            if (typeId == 1)
            {
                ClearCache();
            }
        }
    }
}
